﻿using System;
using System.Linq;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   A console command that provides help information about using the management console.
    /// </summary>
    internal class HelpCommand : Command
    {
        /// <summary>
        ///   The command name parameter.
        /// </summary>
        public const string CommandNameParameter = "command";

        /// <summary>
        ///   The command name parameter (short name).
        /// </summary>
        public const string CommandNameShortParameter = "c";


        /// <summary>
        ///   Gets the console command name.
        /// </summary>
        public override string Name
        {
            get { return "help"; }
        }


        /// <summary>
        ///   Gets the console command description.
        /// </summary>
        public override string Description
        {
            get { return Messages.HelpCommandDescription; }
        }


        /// <summary>
        ///   Gets the console command full description.
        /// </summary>
        public override string FullDescription
        {
            get { return string.Empty; }
        }


        /// <summary>
        ///   Executes the console command.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="context"/> is null.</exception>
        public override void Execute(CommandExecutionContext context)
        {
            base.Execute(context);

            // If no particular command was specified
            if (!context.ParameterValueIsNotEmpty(CommandNameParameter, CommandNameShortParameter))
            {
                // Displaying the hint
                Console.WriteLine(Messages.HelpCommandHintMessage);

                Console.WriteLine();

                // Displaying information about all available commands
                foreach (var command in context.ManagementConsole.CurrentCommandSet.Commands
                    .OrderBy(c => c.Name))
                    Console.WriteLine("{0,-15} {1}", command.Name.ToUpper(), command.Description);
            }
            // If some particular command was specified
            else
            {
                var commandName = context.GetParameterValue(CommandNameParameter, CommandNameShortParameter).ToLower();

                // Searching for the command with the specified name
                var command = context.ManagementConsole.CurrentCommandSet.Commands
                    .FirstOrDefault(c => c.Name == commandName);

                // If the command was found
                if (command != null)
                {
                    // Displaying the command description
                    Console.WriteLine(command.Description);

                    // If the command has the full description
                    if (!string.IsNullOrEmpty(command.FullDescription))
                    {
                        Console.WriteLine();

                        // And then displaying the full description
                        Console.WriteLine(command.FullDescription);
                    }
                }
                // If the command with the specified name was not found
                else
                    // Displaying the error message
                    Console.WriteLine(Messages.NoCommandWithTheSpecifiedName);
            }


            Console.WriteLine();
        }
    }
}
